package com.rr.middle;

/**
 * @author RR
 * @date 2024/7/6 12:31
 */

import java.util.Arrays;

/**
 * https://leetcode.cn/problems/count-alternating-subarrays/
 * o(n)时间复杂度
 */
public class CountAlternatingSubarrays {
    public long countAlternatingSubarrays(int[] nums) {
        int left = 0;
        int pre = 0;
        long result = 1;
        for (int i = 1; i < nums.length; i++) {
            int tmp = 1;
            // 当前数值跟上一个数值不相等的时候tmp的值如下
            // 当数值相等的时候，重置左下标left，tmp=1
            if (nums[pre] != nums[i]) {
                tmp = i - left + 1;
            } else {
                left = i;
            }
            pre++;
            result += tmp;
        }
        return result;
    }

    public static void main(String[] args) {
        CountAlternatingSubarrays c = new CountAlternatingSubarrays();
        System.out.println(c.countAlternatingSubarrays(new int[]{0,1,0,1}));
    }
}
